// 渲染表格(渲染地图)
// 目的：显示到底的方块，此时我们的表格只是一个显示的dom，没有办法实现显示，
// 因为每一帧都要·清除画布，所以要想持久，就需要数据持久起来
// 所以需要一个地类，用来持久到底发方块
(function() {
    window.Map = function() {
        this.mapCode = [
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0],
            [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 0, 0],
            // 再添加一行形成墙来隔绝上面的方块到底后进行继续往下落
            [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
        ]
    };

    Map.prototype.render = function(mapGame) {
        console.log(mapGame);
        // 渲染地图
        for (let i = 0; i < mapGame.row; i++) {
            for (let j = 0; j < mapGame.col; j++) {
                if (this.mapCode[i][j] != 0) {
                    game.setColor(i, j, this.mapCode[i][j])
                }
                $('tr').eq(i).children('td').eq(j).html(this. mapCode[i][j])
            }
        }
    }

    Map.prototype.checkRemove = function() {
        // 判断当前的mapCode是否消行
        // 消行规则：当前的mapCode数组的每一项如果不是0，就说明该消行了
        for (let i = 0; i < 20; i++) {
            // 遍历地图数组进行判断
            if (this.mapCode[i].indexOf(0) == -1) {
                //删除这一行
                this.mapCode.splice(i, 1);
                // 删除一行补一行
                this.mapCode.unshift([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
                // 分数增加,根据不同的速度绝对加多少分
                if (game.during <= 30 && game.during >= 20) {
                    game.score += 10;
                } else if (game.during < 20 && game.during >= 10) {
                    game.score += 20
                } else {
                    game.score += 30
                }
                // 渲染分数
                $('#score').text('分数:' + game.score)
                if (game.score % 100 == 0) {
                    game.during -= 5
                    console.log(game.during);
                    if (game.during <= 0) {
                        game.during = 1
                    }
                }
            }
        }
    }
})()